package com.shenyue.nbtownlet.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shenyue.nbtownlet.R;
import com.shenyue.nbtownlet.base.BaseActivity;
import com.shenyue.nbtownlet.databinding.ActivityChoicePersonnelBinding;
import com.shenyue.nbtownlet.event.BaseListener;
import com.shenyue.nbtownlet.model.AppModel;
import com.shenyue.nbtownlet.model.AppTaskModel;
import com.shenyue.nbtownlet.model.DefaultModel;
import com.shenyue.nbtownlet.treeview.NodeViewFactory;
import com.shenyue.nbtownlet.treeview.TreeNodeEx;
import com.shenyue.nbtownlet.util.StringUtil;

import java.util.List;

import me.texy.treeview.TreeNode;
import me.texy.treeview.TreeView;

/**
 * 流程人员选择
 */

public class ChoicePersonnelActivity extends BaseActivity implements BaseListener {
    private ActivityChoicePersonnelBinding binding;
    private String showtype;
    private String selectRang;
    private String Isfilt;
    private String STREETID;
    private TreeNode root;
    private TreeView treeView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.activity_choice_personnel);
        Bundle bundle = getIntent().getExtras();
        showtype = bundle.getString(DefaultModel.PAGE_DATA);
        selectRang = bundle.getString("selectRang");
        Isfilt = bundle.getString("Isfilt");
        STREETID = bundle.getString("STREETID");
        setActionBar(true, "人员选择");

        root = TreeNode.root();

        binding.progressLayout.showProgress();
        JSONObject param = new JSONObject();
        param.put("rootid", selectRang);
        param.put("showtype", showtype);
        param.put("groupid", AppModel.GROUPID);
        if(StringUtil.isNull(STREETID))
            param.put("Areaid", AppModel.getStreetId(this));
        else
            param.put("Areaid", STREETID);
        param.put("Isfilt", Isfilt);
        AppTaskModel.WaitListGetAllOrgMember(this, param, this);
    }

    private void buildTree(JSONArray data) {
        for (int i = 0, j = data.size(); i < j; i++) {
            JSONObject item = data.getJSONObject(i);
            TreeNodeEx treeNode = new TreeNodeEx(item.getString("title"));
            treeNode.setData(item);
            treeNode.setLevel(0);
            JSONArray childs = item.getJSONArray("childs");
            if (childs != null && !childs.isEmpty()) {
                buildTreeChild(1, childs, treeNode);
            }

            root.addChild(treeNode);
        }

        treeView = new TreeView(root, this, new NodeViewFactory());
        View view = treeView.getView();
        view.setLayoutParams(new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
        binding.treeView.addView(view);
    }

    private void buildTreeChild(int level, JSONArray data, TreeNodeEx parentNo) {
        for (int i = 0, j = data.size(); i < j; i++) {
            JSONObject item = data.getJSONObject(i);
            TreeNodeEx treeNode = new TreeNodeEx(item.getString("title"));
            treeNode.setData(item);
            treeNode.setLevel(level);
            parentNo.addChild(treeNode);
            JSONArray childs = item.getJSONArray("childs");
            if (childs != null && !childs.isEmpty()) {
                buildTreeChild(level + 1, childs, treeNode);
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.submit_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_submit:
                callback();
                break;

        }
        return super.onOptionsItemSelected(item);
    }

    private void callback() {
        if (treeView == null) {
            Toast.makeText(this, "获取人员信息失败", Toast.LENGTH_LONG).show();
            return;
        }
        JSONArray array = getSelectedNodes();
        if (array.isEmpty()) {
            Toast.makeText(this, "请选择人员", Toast.LENGTH_LONG).show();
            return;
        }

        Intent intent = new Intent();
        intent.putExtra(DefaultModel.PAGE_DATA, array.toString());
        setResult(RESULT_OK, intent);
        finish();
    }

    private JSONArray getSelectedNodes() {
        JSONArray list = new JSONArray();
        List<TreeNode> selectedNodes = treeView.getSelectedNodes();
        for (int i = 0; i < selectedNodes.size(); i++) {
            final TreeNodeEx node = (TreeNodeEx) selectedNodes.get(i);
            final JSONObject data = node.getData();
            if("4".equals(data.getString("type"))) {
                list.add(new JSONObject() {{
                    put("ID", StringUtil.dTs(data.getString("id")));
                    put("NAME", node.getValue());
                }});
            }
        }
        return list;
    }

    @Override
    public void success(String val, String action) {
        binding.progressLayout.showContent();
        buildTree(JSON.parseArray(val));
    }

    @Override
    public void error(String error, String action) {
        binding.progressLayout.showContent();
    }
}
